
import time


def trackFile():
    file = r"D:\pythonLearn\文件操作\file01.txt"

    # 打开文件，记录初始位置
    fobj = open(file, mode="rb")
    fobj.seek(0, 2)
    old_position = fobj.tell()

    while True:
        fobj.seek(0, 2)
        new_position = fobj.tell()
        # 判断文件内容增加
        if new_position > old_position:
            fobj.seek(old_position - new_position,2)
            print(fobj.read())
            old_position = new_position
        time.sleep(0.5)

if __name__ == '__main__':
    trackFile()